package com.dengyuanke.upms.logic.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dengyuanke.common.core.result.R;
import com.dengyuanke.common.security.annotation.Inner;
import com.dengyuanke.upms.domain.entity.Log;
import com.dengyuanke.upms.logic.service.ILogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 日志表 前端控制器
 * </p>
 *
 * @author dengyuanke
 * @since 2019-06-29
 */
@RestController
@RequestMapping("/log")
public class LogController {

    @Autowired
    private ILogService logService;

    /**
     * 简单分页查询
     *
     * @param page   分页对象
     * @param sysLog 系统日志
     * @return
     */
    @GetMapping("/page")
    public R getLogPage(Page page, Log sysLog) {
        IPage iPage = logService.page(page, Wrappers.query(sysLog));
        return R.success(iPage);
    }

    /**
     * 删除日志
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_log_del')")
    public R removeById(@PathVariable Long id) {
        boolean remove = logService.removeById(id);
        if (remove) {
            return R.success("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }


    /**
     * 插入日志
     *
     * @param log
     * @return
     */
    @PostMapping
    @Inner
    public R<Boolean> saveLog(@Valid @RequestBody Log log) {
        boolean save = logService.save(log);
        if (save) {
            return R.success();
        } else {
            return R.fail();
        }
    }

}

